package cn.bupt.adt.controller;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.CheckInsti;
import cn.bupt.adt.model.CheckInstitutionModel;
import cn.bupt.adt.service.ICheckInstitutionService;
import cn.bupt.adt.service.impl.CheckInstitutionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/checkInstitutionManagement")
public class CheckInstitutionController {

    @Autowired
    ICheckInstitutionService checkInstitutionService;
    CheckInstitutionService a;

    @RequestMapping("/index/{pageNum}")
    public ModelAndView doctorIndex(@PathVariable("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView(Constants.CHECK_INDEX);
        Page<?> page = PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<CheckInsti> institutions = checkInstitutionService.getAllInstitutions();
        System.out.println(institutions.size());
        mv.addObject("currentPage", page.getPageNum());
        mv.addObject("allPages", page.getPages());
        mv.addObject("institutions", institutions);
        return mv;
    }

    @RequestMapping("/addInstitution")
    public ModelAndView addInstitution() {
        ModelAndView mv = new ModelAndView(Constants.ADD_CHECKINSTITUTION);
        return mv;
    }

    @RequestMapping("/addInstitutionPost")
    public synchronized ModelAndView addInstitutionPost(CheckInsti ins, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("forward:/checkInstitutionManagement/index/1");
        String oper_message = null;
        if (checkInstitutionService.addInstitution(ins, request))
            oper_message = "添加机构成功！";
        else
            oper_message = "添加机构失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/deleteIns/{id}")
    public ModelAndView deleteIns(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView("forward:/checkInstitutionManagement/index/1");
        String oper_message = null;
        if (checkInstitutionService.deleteInstitutionById(id))
            oper_message = "删除机构成功！";
        else
            oper_message = "删除机构失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }

    @RequestMapping("/insDetail/{id}")
    public ModelAndView insDetail(@PathVariable("id") int id) {
        ModelAndView mv = new ModelAndView(Constants.CHECKINSTITUTION_DETAIL);
        CheckInstitutionModel im = checkInstitutionService.getInstitutionById(id);
        mv.addObject("item", im);
        return mv;
    }

    @RequestMapping("/modifyIns")
    public ModelAndView modifyIns(HttpServletRequest request, CheckInsti ins) {
        ModelAndView mv = new ModelAndView("forward:/checkInstitutionManagement/insDetail/" + ins.getId());
        String oper_message = null;
        if (checkInstitutionService.modifyInstitution(ins, request))
            oper_message = "保存成功！";
        else
            oper_message = "保存失败！";
        mv.addObject("oper_message", oper_message);
        return mv;
    }
}
